package com.example.web.controller;

import com.example.web.pojo.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * @program: swagger2-demo
 * @description: Swagger2案例接口
 * @author: ChenZhiXiang
 * @create: 2019-07-21 22:42
 **/
@RestController
@Api(tags="用户相关Controller")
public class SwaggerController {

    @ApiOperation(value = "增加用户", notes = "用户管理页面增加用户,参数为常见类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isAdmin", value = "是否管理员", required = true, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间，格式:Sat, 26 May 2018 10:36:48 GMT", required = false, dataType = "date", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "id:编码，username:用户名，isAdmin:是否管理员，createTime:创建时间")
    })
    @PostMapping("/addUser")
    public Object addUser(@RequestParam(value = "id",required = true) Integer id,
                          @RequestParam(value = "username",required = true) String username,
                          @RequestParam(value = "isAdmin",required = false) boolean isAdmin,
                          @RequestParam(value = "createTime",required = false) Date createTime){
        User user = new User(id,username,isAdmin,createTime);

        return user;
    }

    @ApiOperation(value = "增加用户", notes = "用户管理页面增加用户,参数为对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User", paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "id:编码，username:用户名，isAdmin:是否管理员，createTime:创建时间")
    })
    @PostMapping("/addUser1")
    public Object addUser(@RequestBody User user){
        return user;
    }


    @ApiOperation(value = "上传文件", notes = "参数为文件类型的时候")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "上传成功")
    })
    @PostMapping(value = "/upload",consumes = "multipart/*",headers = "content-type=multipart/form-data")
    public Object upload(@RequestParam("username") String username,@ApiParam(value = "上传的文件",required = true) MultipartFile file){
        return file.getOriginalFilename()+username;
    }

}
